package com.bbzn.device.client.config;

import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.Primary;
import org.springframework.scheduling.concurrent.ThreadPoolTaskExecutor;

import javax.annotation.PostConstruct;
import java.util.concurrent.ThreadPoolExecutor;

@Configuration
public class TranslationThreadPoolConfig {
    ThreadPoolTaskExecutor executor = new ThreadPoolTaskExecutor();
    // 核心线程池大小
    private int corePoolSize = 20;

    // 最大可创建的线程数
    private int maxPoolSize = 50;

    // 队列最大长度
    private int queueCapacity = 1000;

    // 线程池维护线程所允许的空闲时间
    private int keepAliveSeconds = 300;

    //线程名称前缀
    private String threadNamePrefix="ICloud";

    @PostConstruct
    public void init(){
        executor.setMaxPoolSize(maxPoolSize);
        executor.setQueueCapacity(queueCapacity);
        executor.setKeepAliveSeconds(keepAliveSeconds);
        executor.setThreadNamePrefix(threadNamePrefix);
        // 线程池对拒绝任务(无线程可用)的处理策略
        executor.setRejectedExecutionHandler(new ThreadPoolExecutor.CallerRunsPolicy());
    }

    /**
     * 每次新增一个线程池并设定一下核心线程数
     * @return
     */
    @Primary
    @Bean(name = "threadPoolExecutorForVideo")
    public ThreadPoolTaskExecutor translationThreadPoolTaskExecutor()
    {
        executor.setCorePoolSize(corePoolSize);
        return executor;
    }
}